package com.watson.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author watson
 * @date 2021/8/2 10:15
 * @Description: TODO
 **/
public class CompletableFutureDemo {
    public static void main(String[] args) {
        //<Void> 没有返回值的，异步回调，发起一个请求
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("QQQQQQQ");
        });
        System.out.println("111111111");
//        completableFuture.get();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("123123");
            return 1024;
        });
        future.whenComplete((t, u) -> {// 编译成功，正常执行的返回结果
            System.out.println("t===" + t);
            //  表示错误信息
            System.out.println("u===" + u);
        }).exceptionally((e) -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return 233;
        });
    }
}
